console.log(window.wx);
(function (w, d) {
	String.prototype.oss = function () {
      	if (/^http/.test(this)) {
        	return this.toString();
        }
		if (window['__CONFIG'] && window['__CONFIG']['oss']) {
			return (window['__CONFIG']['oss'] + this).toString();
		}
		return this.toString();
	}
	var query = {};
	(function (q) {
		var ps = location.href.split('#')[0].split('?');
		if (ps.length < 2) {
			return;
		}
		var params = ps[1].split('&');
		for (var i = 0; i < params.length; i++) {
			var item = params[i];
			var kv = item.split('=');
			q[kv[0]] = kv[1];
		}
	})(query);
  	function _city() {
    	var dizhi = '';
        if(typeof localAddress != 'undefined') {
          if(typeof localAddress["city"] != 'undefined' && localAddress["city"] != ''){
            dizhi = localAddress["city"];
            dizhi = dizhi.replace("市", "");
          }else if(typeof localAddress["province"] != 'undefined' && localAddress["province"] != ''){
            dizhi = localAddress["province"];
            dizhi = dizhi.replace("省", "");
          }
        }
        w.city = dizhi;
    }
  	var icons = ['💎', '😀', '😍', '😘', '👀', '🐾', '❄', '♐', '⛎', '🎡', '🚴', '🦋', '💍', '💰', '💄', '⚠', '💣', '🚐', '🎀',
				'🎁'
			];
  	var names = ["苟绍辉","宓念蕾","铎照","钞元槐","愈新梅","苦香之","道康成","登思菱","雷冠宇","施正清","缑梦蕊","藏易容","年星腾","阚香蝶","乐睿范","夏侯虎","范晓凡","睦舒荣","奉昂熙","宁乐童","夕冉冉","忻俊侠","干山雁","绳飞雨","斐韶","苌虹","驹问柳","霍杰","阴碧灵","敖梓菱","承巍奕","宜玲玲","爱德海","卜暄和","尧思天","花曼彤","沃湉","枝清华","庄安","夹谷尔容","董亦竹","乘清逸","元业","萨令羽","杜雁露","薛访天","虢凡","郝怡璐","乌游","郁鸿彩","郗易绿","阳梦云","弓畴","呼语心","夙安妮","宰父幼枫","唐傲薇","郜若星","邴春绿","种嘉誉","将书","革绮梦","肇超","隽德馨","系晓蓝","理依霜","赤怀山","凭痴瑶","依念","颜怀山","朱和泰","板水荷","么安澜","成古兰","蔺雅琴","奈睿才","钟离曼蔓","商苓","许苑杰","胡鸿哲","门俊晤","束建本","檀怀梦","佟佳轩","咸傲云","出亦玉","瑞清卓","容修然","闭桃雨","次从阳","始孟阳","侨晗蕾","赏阳辉","母方仪","逢悠雅","崇丹秋","琴麦","信勇军","季秋白","云含云","艾运浩","图门恨寒","利问薇","傅代玉","仲孙修齐","赖良平","浦春娇","库怀薇","完颜尔雅","谢凌丝","燕千柔","俎嘉实","呼延山蝶","伦格","怀问芙","侯子萱","平天元","籍昆锐","牵月朗","富采蓝","冷奇略","禄莎莎","刘高义","泷湛颖","饶向雁","农运凯","尾康适","五曼安","洪代玉","公冶任","莫燕","郸弘厚","富察念梦","滑傲冬","夏彭彭","第五宾","斋浩邈","集掣","廉雨彤","夷夏萱","焉兴贤","甘韦茹","宿萌","智映冬","香明知","第若云","仉夏旋","貊寻芳","莘嘉懿","公良弘阔","镜初阳","雍文昂","聊嘉美","严炫明","折霏霏","东郭问枫","史清卓","任曦","在水冬","节青亦","孟楚楚","贡平","溥雪儿","赵棠","危梓","边米琪","慎朝","敏智宇","祢子晋","凌欣跃","朋书桃","蒿翰海","由采波","浑双文","靖笑天","桑婕","延秀艾","是今歌","问代天","邸宛菡","琦雪卉","旗长平","泉静和","僪涵易","皮丝雨","捷雨晨","佛碧曼","阙琪","尉迟颖","淡纶","扈温文","堵建树","彭之卉","鲜于新美","司雪卉","建梦菲","宏怿","乙冬萱","于悦远","马陶宜","訾湘","太史沈静","斯尹","藩秀妮","白海凡","王俊豪","贝侬","钭景胜","狄宛白","涂以松","真书双","拱略","柏音华","荣念露","禚同甫","吕蓓蕾","麻易巧","玉子骞","慈云","别依云","童樱花","娄梓莹","支幻丝","世雁凡","历和宜","卯滢渟","星心菱","银彬彬","纪忆安","暨芳泽","麦锐逸","顿晴丽","善依琴","嬴夫","逮姗姗","水玄穆","闾一凡","须英韶","桂念巧","桓绍钧","告丽君","诗清舒","蒉安卉","向佳思","罗云飞","皋稷","段雅楠","俞傲霜","姓沛凝","务泰","百里若云","卓觅荷","厉祯","钱春海","脱婷玉","卑维","夫香桃","泰远","昝平安","方初阳","势映菱","盈忆雪","尔伟泽","郎采薇","慕容雯丽","包安青","析海亦","翠昊英","洛霞飞","僧语海","衣兰泽","甲夏容","姒思云","端木柔婉","淳于若兰","嵇希","潭白凡","万俟云心","修流婉","达怀蕾","其美曼","从宏毅","罕和平","步灵秋","硕岑","风蕴","楼乐逸","毋玉轩","六永昌","古馨","山柔淑","粘格菲","梅星文","宣绿蝶","通瑞云","果石","充若英","己竹","终蓝","昔芷蓝","隋安容","乌孙书琴","代妙意","潜珏","答作","声卓","本从蓉","陆茹雪","李清馨","衡颖慧","亓官涵瑶","昂宏峻","羽诗蕊","俟寻桃","仝星河","齐晶滢","招晶辉","箕闲丽","野梓倩","司徒浩穰","象笑槐","扬志泽","针曼彤","操飞双","春成文","井理全","贾以珊","屈恨风","龙阳平","乜晓丝","竹珊","哀凝竹","闽语儿","亥寄容","战映菡","公孙子明","旅柔妙","牟俊民","称灿灿","徭雨梅","笪芦雪","机颖秀","强菱","洋雁芙","计晓骞","学梓琬","鲍玉英","单于旭东","诸采绿","詹香柏","融弘懿","妫翔","苍宇寰","费莫平宁","濯沈雅","完诗珊","空谧辰","丰玮","辟兴平","树鹏池","巴明煦","员元枫","东曼珍","止葛","校致萱","祝安萱","谈绮波","蓝静晨","恽敏","刑芳馨"];
    var tags = ['丨','亅','丿','丶','乚','刂','卜','亻','冫','讠','卩','阝','彳','犭','饣','扌','氵','纟','忄','牜','攵','礻','钅','衤','糹','釒','飠'];
    var chars = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0987654321";
  	var addr = ["附近", "当地", "本地", "同城", "闹市", "私聊", "护士", "白领", "厂妹", "售货员", "文员", "秘书", "公关", "销售", "美容师", "化妆师", "美甲师", "童装", "女装"];
    var common = {
      	Link: function (link) {
            var linkData = link.split('?');
            var link = linkData[0];
            var query = {};
            if (linkData.length == 2) {
                var params = linkData[1].split('&');
                for (var i = 0; i < params.length; i++) {
                    var item = params[i];
                    var kv = item.split('=');
                    query[kv[0]] = kv[1];
                }
            }
            function addParam(name, value) {
                if (arguments.length == 1 && typeof name == 'object') {
                    // 对象
                    for (item in name) {
                        query[item] = name[item];
                    }
                } else {
                    query[name] = value;
                }
            }
            function removeParam(name) {
                delete query[name];
            }
            function getQueryString() {
                var attrs = [];
                for (item in query) {
                    attrs.push(item + '=' + query[item]);
                }
                return attrs.join('&');
            }
            function getUri() {
                return link + '?' + this.getQueryString();
            }
            return {
                addParam: addParam,
                removeParam: removeParam,
                getQueryString: getQueryString,
                getUri: getUri
            };
        },
      	getName: function () {
        	return names[this.rand(0, names.length)];
        },
      	pathUtil: {
        	addParam: function (name, value) {
            	if (arguments.length == 1 && typeof name == 'object') {
                	// 对象
                  	for (item in name) {
                    	query[item] = name[item];
                    }
                } else {
                	query[name] = value;
                }
            },
          	removeParam: function (name) {
            	delete query[name];
            },
          	getQueryString: function () {
              	var attrs = [];
            	for (item in query) {
                	attrs.push(item + '=' + query[item]);
                }
              	return attrs.join('&');
            },
          	getPath: function () {
            	return location.pathname + '?' + this.getQueryString();
            }
        },
      	getCity: function () {
        	return window['city'] || ['成都', '上海', '北京'][this.rand(0, 3)];
        },
      	getConfig: function (configName) {
        	var v = window['__CONFIG'] && window['__CONFIG'][configName];
          	return v || '';
        },
      	getTime: function () {
        	/*
          	var d = new Date();
            d.setMinutes(d.getMinutes() + 5);
            var h = d.getHours() < 10 ? '0' + d.getHours(): d.getHours();
            var m = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes();
            return h + ':' + m;
            */
          	var t1 = this.rand(9, 18);
          	var t2 = this.rand(0, 59);
          	t1 = t1 < 10 ? ('0' + t1) : t1;
          	t2 = t2 < 10 ? ('0' + t2) : t2;
          	return t1 + ':' + t2;
        },
      	getAddr: function () {
        	return addr[this.rand(0, addr.length)];
        },
		getQuery: function (name) {
			return query[name];
		},
      	randContent: function () {
            for (var e = parseInt(60 * Math.random()) + 15, t = 0, a = "", n = 2e4, o = 1e3; t < e;) {
                t++;
                var i = parseInt(Math.random() * o);
                a += String.fromCharCode(n + i)
            }
            return a
        },
		loadCSS: function(src) {
			function load(href) {
				if (!/^http/.test(href)) {
					href = href.oss();
				}
				var link = d.createElement('link');
				link.rel = 'stylesheet';
				link.type = 'text/css';
				link.href = href;
				d.querySelector('head').appendChild(link);
			}
			if (typeof src == 'object') {
				for (var i = 0; i < src.length; i++) {
					var href = src[i];
					load(href);
				}
			} else {
				load(src);
			}
			
		},
        loadJS: function (src, cb) {
          	/*
          	if (/sdk\/report/.test(src)) {
              	if (typeof cb == 'function') {
                	cb();
                }
            	return;
            }
            */
			function load(laodSrc, loadCB) {
				if (!/^http/.test(laodSrc)) {
					laodSrc = laodSrc.oss();
				}
				var script = d.createElement('script');
				script.src = laodSrc;
				if (typeof loadCB == 'function') {
					script.onload = loadCB;
				}
				d.querySelector('head').appendChild(script);
			}
			if (typeof src == 'object') {
				for (var i = 0; i < src.length; i++) {
					var url = src[i];
					load(url);
				}
			} else {
				load(src, cb);
			}
        },
        rand: function (min, max) {
			if (arguments.length == 0) {
				return 0;
			}
            if (arguments.length == 1) {
                max = min;
                min = 0;
            }
          	if (isNaN(min)) {
            	min = 0;
            } else {
            	min = parseInt(min);
            }
          	if (isNaN(max)) {
            	max = min + 10;
            } else {
            	max = parseInt(max);
            }
            return Math.floor(Math.random() * (max - min) + min);
        },
        randStr: function (len, mlen) {
            var str = '';
			if (arguments.length == 2) {
				len = this.rand(len, mlen);
			}
            for (var i = 0; i < len; i++) {
                str += chars.charAt(this.rand(chars.length));
            }
            return str;
        },
		initCity: function (cb) {
          	if(typeof localAddress != 'undefined') {
            	_city();return;
            }
			this.loadJS('https://ip.ws.126.net/ipquery', function () {
				_city();
              	typeof cb == 'function' && cb()
			})
		},
		get: function (url, callback, isJSON) {
			if (arguments.length == 2) {
				isJSON = false;
			}
            var request = new XMLHttpRequest();
            request.open('get', url, true);
            request.send();
            request.onreadystatechange = function () {
                if (request.readyState === 4 && request.status === 200) {
                    isJSON ? callback(JSON.parse(request.responseText)) : callback(request.responseText);
                }
            }
        },
		post: function (url, data, callback, isJSON) {
			if (arguments.length == 3) {
				isJSON = false;
			}
			var request = new XMLHttpRequest();
			request.open('post', url, true);
			request.send(data);
			request.onreadystatechange = function () {
			    if (request.readyState === 4 && request.status === 200) {
			        isJSON ? callback(JSON.parse(request.responseText)) : callback(request.responseText);
			    }
			}
		},
		sign: function (api, cb) {
			this.get(api, cb, api.indexOf('code=de') > 0);
		},
		_wxReady: function () {
			console.log('wx ready');
		},
      	insertSymbol: function (s, r, t) {
          	if (!s || s.length == 0) return icons[Math.floor(Math.random() * icons.length)];
			if (!s || s.length < 2) return s;
			var arr = s.split('');
			if (arguments.length == 3) {
				if (!t || t < 1) t = 1;
				if (r >= s.length) {
					r = s.length - 1;
				} else if (r < 1) {
					r = 1;
				}
				for (var i = 0, l = t; i < l; i++) {
					var icon = icons[Math.floor(Math.random() * icons.length)];
					arr.splice(r, 0, icon);
				}
				return arr.join('');
			}
			if (!r || r < 1) r = 1;
			for (var i = 0, l = r; i < l; i++) {
				var icon = icons[Math.floor(Math.random() * icons.length)];
				var index = Math.floor(Math.random() * arr.length);
              	if (index >= arr.length) {
					index = arr.length - 1;
				} else if (index < 1) {
					index = 1;
				}
				arr.splice(index, 0, icon);
			}
			return arr.join('');
		},
		insertTag: function (s, r, t) {
          	if (!s || s.length == 0) return tags[Math.floor(Math.random() * tags.length)];
			if (!s || s.length < 2) return s;
			var arr = s.split('');
			if (arguments.length == 3) {
				if (!t || t < 1) t = 1;
				if (r >= s.length) {
					r = s.length;
				} else if (r < 1) {
					r = 0;
				}
				for (var i = 0, l = t; i < l; i++) {
					var icon = tags[Math.floor(Math.random() * tags.length)];
					arr.splice(r, 0, icon);
				}
				return arr.join('');
			}
			if (!r || r < 1) r = 1;
			for (var i = 0, l = r; i < l; i++) {
				var icon = tags[Math.floor(Math.random() * tags.length)];
				var index = Math.floor(Math.random() * arr.length);
              	if (index >= arr.length) {
					index = arr.length;
				} else if (index < 1) {
					index = 0;
				}
				arr.splice(index, 0, icon);
			}
			return arr.join('');
		},
		initShare: function (cb) {
			// 加载签名js
			var self = this;
			if (!w.wx) {
				common.loadJS('https://res.wx.qq.com/open/js/jweixin-1.0.0.js', function () {
					self.wxConfig();
					cb();
				});
			} else {
				self.wxConfig();
				cb();
			}
			
		},
		wxConfig: function () {
			w.wx.init = function (api) {
				// 获取签名信息
				common.sign(api, function (data) {
					wx.data = data;
					common._wxReady();
				});
			}
			w.wx.onReady = function (cb) {
				common._wxReady = cb;
			}
		}
    }
    w.Common = common;
  	String.prototype.symbol = function (t, r) {
		if (arguments.length == 2) {
			return common.insertSymbol(this, t, r);
		}
		return common.insertSymbol(this, t);

	}
	String.prototype.tag = function (t, r) {
		if (arguments.length == 2) {
			return common.insertTag(this, t, r);
		}
		return common.insertTag(this, t);

	}
  	String.prototype.format = function (obj) {
      var s = this.toString();
      for (name in obj) {
        var re = new RegExp("{" + name + "}", 'g');
        s = s.replace(re, obj[name]);
      }
      return s;
    }
	w.log = {
		push: function (arr) {
			if (!arr || arr.length < 3) return;
          	// if(!arr[1].startsWith('朋友圈')) return;
			var data = [
				"event=" + encodeURIComponent(arr[0]),
				"tag=" + encodeURIComponent(arr[1]),
				"data=" + encodeURIComponent((typeof arr[2] == 'object' ? JSON.stringify(arr[2]) : arr[2])),
			]
			// common.loadJS('https://po.hxiiz.cn/share/log?' + data.join('&'));
		}
	};
})(window, document);